--火愿焚尽
local huoyuanfenjin = fk.CreateSkill {
    name = "mcdh_huoyuanfenjin"
}

huoyuanfenjin:addEffect('active',{
    interaction = function(self,player)
        local choices = {}
        local all_choices = {"mcdh_HYFJ_fireslash","mcdh_HYFJ_getcard","mcdh_HYFJ_beishui"}
        local room = Fk:currentRoom()
        local max_num = -1
        local maxhandcardplayers = {}
        for _, p in ipairs(room.alive_players) do
                local num = #p:getCardIds(Player.Hand)
                if max_num < num then
                    maxhandcardplayers = {}
                    max_num = num
                end
                if max_num == num then
                    table.insert(maxhandcardplayers,p)
                end
            end
            

        local players =table.filter(maxhandcardplayers,function (p)
            return player:canUseTo(Fk:cloneCard("fire__slash"),p,{bypass_distances=true,bypass_times=true})
        end)

        if #players > 0 then
            table.insert(choices,"mcdh_HYFJ_fireslash")
        end



        local players =table.filter(maxhandcardplayers,function (p)
            return p:getHandcardNum() > 0 and player ~= p
        end)
       if #players > 0 then
        table.insert(choices,"mcdh_HYFJ_getcard")
       end
        
        table.insert(choices,"mcdh_HYFJ_beishui")
        return UI.ComboBox {
            choices = choices,all_choices = all_choices
        }
    end,
    can_use = function (self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,

    prompt = function (self,player, selected_cards, selected_targets)
        if self.interaction.data then
            if self.interaction.data == "mcdh_HYFJ_beishui" then
                return "#mcdh_HYFJ_beishui_prompt"
            elseif self.interaction.data == "mcdh_HYFJ_fireslash" then
                return "#mcdh_HYFJ_fireslash_prompt"
            elseif self.interaction.data == "mcdh_HYFJ_getcard" then
                return "#mcdh_HYFJ_getcard_prompt"
            end
        end
        return ""
    end,
    target_filter = function (self, player,to_select, selected, selected_cards)
        if self.interaction.data then
            local room = Fk:currentRoom()
            
            if self.interaction.data == "mcdh_HYFJ_beishui" then
                return false
            end

            if #selected == 1 then
                return false
            end
            local maxhandcardplayers = {}
            local max_num = -1
            for _, p in ipairs(room.alive_players) do
                local num = #p:getCardIds(Player.Hand)
                if max_num < num then
                    maxhandcardplayers = {}
                    max_num = num
                end
                if max_num == num then
                    table.insert(maxhandcardplayers,p)
                end
            end

        
            if self.interaction == "mcdh_HYFJ_fireslash" then
                 return table.contains(maxhandcardplayers,to_select) and player:canUseTo(Fk:cloneCard("fire__slash"),to_select,{bypass_distances=true,bypass_times=true})
            elseif self.interaction == "mcdh_HYFJ_getcard" then
                return   table.contains(maxhandcardplayers,to_select) and not to_select:isKongcheng() and to_select ~= player
            end
           
        end
    end,
    card_filter = function (self, player,to_select, selected)
        if self.interaction.data then
            if self.interaction.data ~= "mcdh_HYFJ_beishui" or #selected >= 2 then
                return false
            end
            return true
        end
    end,
    feasible = function (self, player,selected, selected_cards)
        if self.interaction.data then
            if self.interaction.data == "mcdh_HYFJ_beishui" then
                return #selected == 0 and #selected_cards == 2
            else
                return #selected == 1 and #selected_cards == 0
            end
        end
    end,
    on_use = function (self, room, use)
        if self.interaction.data == "mcdh_HYFJ_beishui" then
            local from = use.from
            local card_ids = use.cards

            room:askToYiji(from,{targets=room:getOtherPlayers(from),min_num = 1 ,cancelable=false,max_num=1,skill_name = huoyuanfenjin.name,cards=card_ids})

            if from:canUse(Fk:cloneCard("fire__slash")) then
                local maxhandcardplayers = {}
                local max_num = -1
                for _, p in ipairs(room.alive_players) do
                    local num = #p:getCardIds(Player.Hand)
                    if max_num < num then
                        maxhandcardplayers = {}
                        max_num = num
                    end
                    if max_num == num then
                        table.insert(maxhandcardplayers,p)
                    end
                end
                local players =table.filter(maxhandcardplayers,function (p)
                    return from:canUseTo(Fk:cloneCard("fire__slash"),p,{bypass_distances=true,bypass_times=true})
                end)
                if #players > 0 then
                    local tos = room:askToChoosePlayers(from,{targets=players,min_num=1,max_num=1,prompt="#mcdh_HYFJ_askfor_fireslash",cancelable=false,skill_name=self.name})
                    room:useVirtualCard("fire__slash",nil, from, tos, self.name)
                end
            end
            local maxhandcardplayers = {}
            local max_num = -1
            for _, p in ipairs(room.alive_players) do
                local num = #p:getCardIds(Player.Hand)
                if max_num < num then
                    maxhandcardplayers = {}
                    max_num = num
                end
                if max_num == num then
                    table.insert(maxhandcardplayers,p)
                end
            end

            local players =table.filter(maxhandcardplayers,function (p)
                return not p:isKongcheng() and from ~= p
            end)

            if #players > 0 then
                local tos = room:askToChoosePlayers(from,{targets = players,min_num=1,max_num=1,skill_name=self.name,prompt="#mcdh_HYFJ_askfor_getcard",cancelable=false})
                local to = tos[1]
                local c = room:askForCardChosen(from, to, "h", self.name,"#mcdh_HYFJ_gettargetcard")
                room:obtainCard(from, c, false, fk.ReasonPrey)
            end
        elseif self.interaction.data == "mcdh_HYFJ_fireslash" then
            local from = use.from
            room:useVirtualCard("fire__slash",nil, from, use.tos, self.name)
        elseif self.interaction.data == "mcdh_HYFJ_getcard" then
            local from = use.from
            local to = use.tos[1]
            local c = room:askForCardChosen(from, to, "h", self.name,"#mcdh_HYFJ_gettargetcard")
            room:obtainCard(from, c, false, fk.ReasonPrey)
        end
    end
})
Fk:loadTranslationTable {
    ["mcdh_huoyuanfenjin"] = "愿焰",
    [":mcdh_huoyuanfenjin"] = "『火愿焚尽』<br>出牌阶段限一次，你可以选择一项:1.视为对全场手牌数最多的一名角色使用一张火【杀】（无距离限制）;2.获得全场手牌数最多的一名角色的一张手牌;3.将两张牌交给任意名其他角色，然后依次执行前两项。",
    ["mcdh_HYFJ_fireslash"] = "视为对全场手牌数最多的一名角色使用一张火【杀】",
    ["mcdh_HYFJ_getcard"] = "获得全场手牌数最多的一名角色的一张手牌",
    ["mcdh_HYFJ_beishui"] = "将两张牌交给任意名其他角色，然后依次执行前两项",
    ["#mcdh_HYFJ_fireslash_prompt"] = "火愿焚尽：视为对全场手牌数最多的一名角色使用一张火【杀】",
    ["#mcdh_HYFJ_getcard_prompt"] = "火愿焚尽：获得全场手牌数最多的一名角色的一张手牌",
    ["#mcdh_HYFJ_beishui_prompt"] = "火愿焚尽：将两张牌交给任意名其他角色，然后依次执行前两项",
    ["#mcdh_HYFJ_gettargetcard"] = "火愿焚尽：获得一张手牌",
    ["#mcdh_HYFJ_askfor_fireslash"] = "火愿焚尽：视为对全场手牌数最多的一名角色使用一张火【杀】",
    ["#mcdh_HYFJ_askfor_getcard"] = "火愿焚尽：获得全场手牌数最多的一名角色的一张手牌",
}

return huoyuanfenjin